package com.quickcode.example.juc;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.BiFunction;

/**
 * 计算结果合并
 */
@Slf4j
public class CompletableFutureExample4 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CompletableFuture<Integer> server1 = CompletableFuture.supplyAsync(() -> {
            return 1 + 2;
        });

        CompletableFuture<Integer> server2 = CompletableFuture.supplyAsync(() -> {
            return 3 + 4;
        });

        CompletableFuture<Integer> result = server1.thenCombine(server2, new BiFunction<Integer, Integer, Integer>() {

            @Override
            public Integer apply(Integer integer, Integer integer2) {
                return integer + integer2;
            }
        });

        log.info("result:{}",result.get());
    }
}